package com.study.talk.ui.app.login.fragment

import android.app.DatePickerDialog
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.study.talk.R
import com.study.talk.ui.app.base.BaseFragment
import com.study.talk.databinding.FragmentSetUserInfoBinding
import com.study.talk.utils.FileUtil
import com.study.talk.ui.app.login.LoginViewModel
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SetUserInfoFragment : BaseFragment() {
    private val takePhoto =
        registerForActivityResult(ActivityResultContracts.TakePicture()) { result: Boolean ->
            if (result) {
                uri?.let { vm.upload(FileUtil.getPathFromUri(requireContext(), it)) }
            }
        }
    private val selectPhoto =
        registerForActivityResult<PickVisualMediaRequest, Uri>(ActivityResultContracts.PickVisualMedia()) { uri: Uri? ->
            uri?.let { vm.upload(FileUtil.getPathFromUri(requireContext(), uri)) }
        }
    private val vm: SetUserInfoViewModel by viewModels()

    private val parentViewModel by lazy { ViewModelProvider(requireActivity())[LoginViewModel::class] }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        val binding = DataBindingUtil.inflate<FragmentSetUserInfoBinding>(
            inflater, R.layout.fragment_set_user_info, container, false
        )
        initView(binding)
        return binding.root
    }

    private fun initView(binding: FragmentSetUserInfoBinding) {

        parentViewModel.loginResponse.value?.let {
            vm.loadResponse(it)

            binding.vm = vm
            binding.lifecycleOwner = this

            vm.complete.observe(viewLifecycleOwner) { info ->
                info?.let { userInfo ->
                    parentViewModel.changUserInfo(userInfo)
                    showToast("信息初始化成功")
                    findNavController().navigate(R.id.action_setUserInfoFragment_to_loadingFragment)
                }
            }

            binding.registerImg.setOnClickListener {
                showSelectPhotoDialog(
                    selectPhoto, takePhoto
                )
            }

            binding.registerBirth.setOnClickListener {
                val dialog = DatePickerDialog(requireActivity())
                dialog.show()
                dialog.setOnDateSetListener { _, year: Int, month: Int, day: Int ->
                    vm.changeBirth(year, month, day)
                }
            }

            binding.registerBt.setOnClickListener {
                if (vm.check()) {
                    vm.submit()
                }
            }

            binding.genderGroup.setOnCheckedChangeListener { group, checkedId ->
                if (group.checkedRadioButtonId != checkedId) {
                    group.check(checkedId)
                }
                if (checkedId == binding.registerManBt.id) {
                    vm.gender.postValue(1)
                } else {
                    vm.gender.postValue(0)
                }
            }
        }
        if (parentViewModel.loginResponse.value == null) {
            showToast("数据错误")
        }
        return
    }
}